package ink.xiaobaibai.serviceOxy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-06-11
 **/

@Service
public class RedisUtils implements IRedisUtils {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 优惠券码
     */
    private static final String COUPON_CODE_ORDER = "_couponCode:";

    /**
     * 优惠券
     */
    private static final String COUPON_ORDER = "_coupon:";

    /**
     * 运行时间key
     */
    private static final String FUNCTION_TIME_KEY = "function-time:";


    @Override
    public void saveCoupon(String orderId, Integer couponId) {
        this.stringRedisTemplate.opsForValue().set(COUPON_ORDER + orderId, couponId.toString(), 15, TimeUnit.MINUTES);
    }

    @Override
    public void saveCouponCode(String orderId, String couponCode) {
        this.stringRedisTemplate.opsForValue().set(COUPON_CODE_ORDER + orderId, couponCode, 15, TimeUnit.MINUTES);
    }

    @Override
    public Integer getCoupon(String orderId) {
        String s = this.stringRedisTemplate.opsForValue().get(COUPON_ORDER + orderId);
        if (s == null) {
            return null;
        }
        return Integer.parseInt(s);
    }

    @Override
    public String getCouponCode(String orderId) {
        return this.stringRedisTemplate.opsForValue().get(COUPON_CODE_ORDER + orderId);
    }

    @Override
    public void saveOnePatFunctionTime(String orderId, Integer time) {
        this.stringRedisTemplate.opsForValue().set(FUNCTION_TIME_KEY + orderId, String.valueOf(time), 5, TimeUnit.MINUTES);
    }

    @Override
    public String getOnePatFunctionTime(String orderId) {
        return this.stringRedisTemplate.opsForValue().get(FUNCTION_TIME_KEY + orderId);
    }

}
